
package cn.batch.daemon.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.batch.daemon.taskEnum.LoanStatus;
import cn.batch.daemon.taskEnum.LoanType;

/**
 * @author xihn
 * 解析人行征信报告中的贷款信息字符
 */
public class AnalysisLoan {

	private static String splitPoint = "。";
	
	private static String splitComma = "，";
	/**
	 * 解析贷款汇总信息，取得贷款种类、贷款状态、贷款剩余期数(月)
	 * @param s
	 * @return
	 * @throws ParseException 
	 */
	public static Map<String,String> getLoanType(String s) throws ParseException{
		Map<String,String> rm = new HashMap<String,String>();
		LoanType[] loanTypes = LoanType.ALL;
		LoanStatus[] loanStatus = LoanStatus.ALL;
		String [] sForPoint = s.split(splitPoint);
		String loanT = sForPoint[0];
		String []lt = loanT.split(splitComma); 
		
		//获得贷款发放日期
		String yearL = loanT.split("\\.")[1].substring(0, 4);
		String monthL = loanT.split("\\.")[1].substring(5, 7);
		String dayL = loanT.split("\\.")[1].substring(8, 10);
		rm.put("loanOccureDate", yearL+"-"+monthL+"-"+dayL);
		
		//获得贷款发放金额
		String t = loanT.split("发放的")[1];
		String t2 = t.split("元")[0];
		rm.put("loanMoney", t2);
		
		for (int i = 0; i < lt.length; i++) {
			String st = lt[i];
			for (int j = 0; j < loanTypes.length; j++) {
				LoanType lp = loanTypes[j];
				if(st.endsWith(lp.getDisplayText())){
					//贷款种类
					rm.put("loanType", lp.getValue());
					break;
				}
			}
			
			if(i==lt.length-1){
				rm.put("loanDueTime",lt[i]);
			}
		}
		
		String loanS = sForPoint[1];
		String []ls = loanS.split(splitComma);
		for (int i = 0; i < ls.length; i++) {
			String ss = ls[i];
			for (int j = 0; j < loanStatus.length; j++) {
				LoanStatus lss = loanStatus[j];
				if(ss.endsWith(lss.getDisplayText())){
					//贷款状态
					rm.put("loanStauts", lss.getValue());
					break;
				}
			}
		}
		
		String loanDueTime = "";
		String[] sp = rm.get("loanDueTime").toString().split(",");
		if(sp.length>1){
			loanDueTime  = sp[1];
		}else{
			loanDueTime  = sp[0];
		}
		
		String year = loanDueTime.substring(0, 4);
		String month = loanDueTime.substring(5, 7);
		String day = loanDueTime.substring(8, 10);
		//贷款到期日期
		rm.put("payOverDate", year+"-"+month+"-"+day);
		
		//如无对应贷款种类信息，将贷款种类设为[其他]
		if(rm.get("loanType")==null)
			rm.put("loanType", LoanType.QT.getValue());
		//如无对应贷款状态信息，将贷款状态设为[正常]
		if(rm.get("loanStauts")==null)
			rm.put("loanStauts", LoanStatus.ZC.getValue());
		//如贷款状态为结清或呆账，将贷款还款剩余期数设为 0
		if(rm.get("loanStauts").toString().equals(LoanStatus.JQ.getValue())){
			rm.put("loanDueNum","0");
		}else{
			//如贷款状态非结清，则解析贷款到期字符串，获得到期日期，用现日期-到期日期，获得剩余月数
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");			
			
			int months = DateUtil.getMonths(new Date(), df.parse(year+"-"+month+"-"+day));
			
			rm.put("loanDueNum",String.valueOf(months));
		}
		
		// add by liuyin 20130131 begin
		// 把描述中的余额截取出来，存入 本金余额字段
		Pattern p = Pattern.compile(".*余额为(.*)元.*");
		Matcher m = p.matcher(s);
		while (m.find()) {
			rm.put("capitalBalance", m.group(1));
		}
		// add by liuyin 20130131 end
		
		return rm;
	}
	/**
	 * 截取银行类型
	 * @param s
	 * @return
	 * @throws ParseException
	 */
	public static String getComapanyType(String s) throws ParseException{
		
		String comType;
		//截取银行类型
		String t = s.split("”")[0];
		String t2 = t.split("“")[1];
		comType =  t2;
		return comType;
	}
}
